package com.molichuxing.gateway.bff.payment.services.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.molichuxing.framework.utils.DingTalkUtil;
import com.molichuxing.framework.utils.XmlUtil;
import com.molichuxing.gateway.bff.payment.services.WechatpayBffService;
import com.molichuxing.services.business.dto.response.NotifyBizDto;
import com.molichuxing.services.business.service.PaymentBizService;
import com.molichuxing.services.property.PaymentChannelEnum;

/**
 * 微信支付bff
 * 
 * @author zoumingyu
 * @date 2019年10月29日
 *
 */
@Service("wechatpayBffService")
public class WechatpayBffServiceImpl implements WechatpayBffService {
	private static final Logger logger = LoggerFactory.getLogger(WechatpayBffServiceImpl.class);

	@Value("${profiles.active}")
	private String active;

	@Value("${profiles.active.explain}")
	private String activeExplain;

	@Resource
	private PaymentBizService paymentBizService;

	/**
	 * 微信支付回调结果
	 * 
	 * @param xmlStr
	 * @return
	 * @throws Exception
	 */
	@Override
	public NotifyBizDto modifyNotifyPay(String xmlStr) throws Exception {
		logger.info("[modifyNotifyPay]微信支付回调:{}", xmlStr);
		NotifyBizDto result = null;
		// 验证并修改支付单信息
		try {
			Map<String, String> noifyParam = XmlUtil.xmlToMap(xmlStr);
			noifyParam.put("xmlStr", xmlStr);
			result = paymentBizService.modifyNotify(noifyParam, PaymentChannelEnum.WXPAY);
			logger.info("[modifyNotifyPay]微信支付修改结果：" + JSONObject.toJSONString(result));
		} catch (Exception e) {
			logger.error("[modifyNotifyPay]微信支付回调异常:", e);
			DingTalkUtil.exceptionSend(activeExplain + "微信支付回调异常：" + e.getMessage());
			throw e;
		}
		return result;
	}
}
